﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 充值窗口Ex : Form
    {
        public 充值窗口Ex()
        {
            InitializeComponent();
        }
        private MySqlHelper.ManagerInfo m_ManagerInfo = new MySqlHelper.ManagerInfo();
        public static 充值窗口Ex create(MySqlHelper.ManagerInfo info, 客服窗口 window)
        {
            充值窗口Ex c = new 充值窗口Ex();
            c.m_ManagerInfo = info;
            c.ShowDialog(window);
            return c;
        }
        private string Name, userNumber, phoneNumber;

        private void buttonNext_Click(object sender, EventArgs e)
        {
            DataBase.Error er = DataBase.getInstance().isUserId(textBox充值QQ.Text,out userNumber, out Name,out phoneNumber);
            if (er == DataBase.Error.ok)
            {
                //if (MessageBox.Show("检测到您的姓名为" + Name + "是否继续?", "", MessageBoxButtons.OKCancel) != DialogResult.OK)
                //    return;
                textBox姓名.Text = Name;
                tabControl1.SelectedTab = tabPage2;
            }
            else if (er == DataBase.Error.id)
            {
                MessageBox.Show("该账号不存在!");
                return;
            }


            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var hint = string.Format("您的账号：{0}\r\n姓名：{1}\r\n请确认是否准确！",
                    textBox充值QQ.Text, textBox姓名.Text);
            Clipboard.SetDataObject(hint);
            string m_lpstrId = textBox充值QQ.Text;
            string m_lpstrMoney = comboBox充值金额.SelectedItem.ToString();
            ////////////////// 提示充值者信息  //////////////
            string stringbuild = "\n 充值账号:" + m_lpstrId + "   |   充值金额:" + m_lpstrMoney + ".00 元\n充值者姓名:" + Name;
            if (MessageBox.Show("确认充值吗？" + stringbuild, "人工充值", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                // 进入充值
                // 填充结构体
                DataBase.PayInfo info = new DataBase.PayInfo();
                info.managerId = m_ManagerInfo.id;
                info.managerName = m_ManagerInfo.name;
                info.money = Convert.ToDouble(m_lpstrMoney);
                info.time = DateTime.Now;
                info.userId = m_lpstrId;
                info.userName = Name;
                info.userNumber = userNumber;
                info.userPhoneNumber = phoneNumber;
                DataBase.Error error = DataBase.getInstance().addPayInfo(info);//建档
                error = DataBase.getInstance().addUserMoney(m_lpstrId, info.money);
                if (error == DataBase.Error.ok)
                {
                    double money;
                    DataBase.getInstance().getUserMoney(m_lpstrId,out money);
                    MessageBox.Show("充值成功!");
                    hint = string.Format("亲爱的{0}\r\n您的{1}元打印费充值成功！\r\n现在余额{2}元\r\n谢谢您对懒人自主打印的支持!",
                    textBox姓名.Text,info.money.ToString("0.00"),money);
                    Clipboard.SetDataObject(hint);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("充值失败!"+error);
                    return;
                }
                string msg = string.Format("为客户[{0}]充值{1}元", info.userName, info.money.ToString("0.00"));
                Log.managerLog(msg, m_ManagerInfo.id, m_ManagerInfo.name, m_ManagerInfo.user);
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }
    }
}
